package com.hdu.other.dp.gupiao;

public class lc309 {
    class Solution {
        public int maxProfit(int[] prices) {

            int len = prices.length;
            if (len < 2) {
                return 0;
            }
            int[] done = new int[len];
            int[] prepare = new int[len];

            done[1] = Math.max(prices[1] - prices[0], 0);
            prepare[1] = Math.max(-prices[0], -prices[1]);

            for (int i = 2; i < len; i++) {
                done[i] = Math.max(done[i - 1], prepare[i - 1] + prices[i]);
                prepare[i] = Math.max(done[i - 2] - prices[i], prepare[i - 1]);
            }
            return done[len - 1];
        }
    }
}
